
package com.platform.modules.mall.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.platform.common.annotation.SysLog;
import com.platform.common.utils.CharUtil;
import com.platform.common.utils.RestResponse;
import com.platform.modules.mall.entity.MallScratchCardsEntity;
import com.platform.modules.mall.service.MallScratchCardsService;
import com.platform.modules.sys.controller.AbstractController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 刮刮卡数据Controller
 *
 * @author
 * @date 2021-12-01 11:40:07
 */
@RestController
@RequestMapping("mall/scratchcards")
public class MallScratchCardsController extends AbstractController {
    @Autowired
    private MallScratchCardsService mallScratchCardsService;

    /**
     * 查看所有列表
     *
     * @param params 查询参数
     * @return RestResponse
     */
    @RequestMapping("/queryAll")
    @RequiresPermissions("mall:scratchcards:list")
    public RestResponse queryAll(@RequestParam Map<String, Object> params) {
        List<MallScratchCardsEntity> list = mallScratchCardsService.queryAll(params);

        return RestResponse.success().put("list", list);
    }

    /**
     * 分页查询刮刮卡数据
     *
     * @param params 查询参数
     * @return RestResponse
     */
    @GetMapping("/list")
    @RequiresPermissions("mall:scratchcards:list")
    public RestResponse list(@RequestParam Map<String, Object> params) {
        Page page = mallScratchCardsService.queryPage(params);

        return RestResponse.success().put("page", page);
    }

    /**
     * 根据主键查询详情
     *
     * @param id 主键
     * @return RestResponse
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("mall:scratchcards:info")
    public RestResponse info(@PathVariable("id") String id) {
        MallScratchCardsEntity mallScratchCards = mallScratchCardsService.getById(id);

        return RestResponse.success().put("scratchcards", mallScratchCards);
    }

    /**
     * 新增刮刮卡数据
     *
     * @param mallScratchCards mallScratchCards
     * @return RestResponse
     */
    @SysLog("新增刮刮卡数据")
    @RequestMapping("/save")
    @RequiresPermissions("mall:scratchcards:save")
    public RestResponse save(@RequestBody MallScratchCardsEntity mallScratchCards) {

        mallScratchCardsService.add(mallScratchCards);

        return RestResponse.success();
    }

    /**
     * 批量新增刮刮卡数据
     *
     * @param mallScratchCards mallScratchCards
     * @return RestResponse
     */
    @SysLog("批量新增刮刮卡数据")
    @RequestMapping("/saveBatch")
    @RequiresPermissions("mall:scratchcards:save")
    public RestResponse saveBatch(@RequestBody MallScratchCardsEntity mallScratchCards) {

        List<MallScratchCardsEntity> list = new ArrayList<>(mallScratchCards.getNumber());
        MallScratchCardsEntity entity;
        for (int i = 0; i < mallScratchCards.getNumber(); i++) {
            entity = new MallScratchCardsEntity();
            entity.setSn(mallScratchCards.getSn());
            entity.setCode(CharUtil.getRandomString(6).toUpperCase());
            list.add(entity);
        }
        mallScratchCardsService.saveBatch(list);

        return RestResponse.success();
    }


    /**
     * 修改刮刮卡数据
     *
     * @param mallScratchCards mallScratchCards
     * @return RestResponse
     */
    @SysLog("修改刮刮卡数据")
    @RequestMapping("/update")
    @RequiresPermissions("mall:scratchcards:update")
    public RestResponse update(@RequestBody MallScratchCardsEntity mallScratchCards) {

        mallScratchCardsService.update(mallScratchCards);

        return RestResponse.success();
    }

    /**
     * 根据主键删除刮刮卡数据
     *
     * @param ids ids
     * @return RestResponse
     */
    @SysLog("删除刮刮卡数据")
    @RequestMapping("/delete")
    @RequiresPermissions("mall:scratchcards:delete")
    public RestResponse delete(@RequestBody String[] ids) {
        mallScratchCardsService.deleteBatch(ids);

        return RestResponse.success();
    }
}
